package cn.com.hhrcw.personal.cms.web;

import cn.com.hhrcw.entity.CmsChannel;
import cn.com.hhrcw.personal.cms.mapstruct.CmsChannelConvert;
import cn.com.hhrcw.personal.cms.params.CmsChannelParams;
import cn.com.hhrcw.personal.cms.vo.CmsChannelVO;
import cn.com.hhrcw.service.ICmsChannelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.function.Function;

/**
* 栏目
*
* @author lixingxing
* @since 2020-11-06
*/
@RestController("cmsChannelPersonalController")
@Slf4j
@RequestMapping("/personal/cmsChannel")
@Api(tags = "栏目")
@Validated
public class CmsChannelController extends JeecgController<CmsChannel, ICmsChannelService> {

    @Autowired
    private ICmsChannelService cmsChannelService;

    @Autowired
    private CmsChannelConvert cmsChannelConvert;


    /**
    * 分页列表查询
    *
    * @param params 请求参数
    * @return
    */
    @GetMapping(value = "/list")
    @AutoLog("栏目-分页列表查询")
    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    public Result<Page<CmsChannelVO>> list(CmsChannelParams params, HttpServletRequest request) {
        if(StringUtils.isBlank(params.getParentId())){
            //去除一级栏目
            params.setParentId("0");
        }
        QueryWrapper<CmsChannel> queryWrapper = QueryGenerator.initQueryWrapper(cmsChannelConvert.toEntity(params), request.getParameterMap());
        queryWrapper.eq("del_flag", false);
        if (StringUtils.isBlank(params.getField())) {
            queryWrapper.orderByAsc("sort_order");

        } else {
            queryWrapper.orderBy(true, params.getSort(), toUnderlineName(params.getField()));
        }
        IPage<CmsChannel> page = cmsChannelService.page(params.page(), queryWrapper);
        //分页结果转换器
        Function<CmsChannel, CmsChannelVO> fun = cmsChannel -> cmsChannelConvert.toVO(cmsChannel);

        return Result.ok((Page)page.convert(fun));
    }

    /**
    * 根据id查询
    *
    * @param id 数据id
    * @return
    */
    @GetMapping(value = "/queryById")
    @ApiOperation(value = "根据id查询", notes = "根据id查询")
    public Result<CmsChannelVO> queryById(String id) {
        CmsChannel cmsChannel = cmsChannelService
            .lambdaQuery()
            .eq(CmsChannel::getId,id)
            .eq(CmsChannel::getDelFlag,false)
            .one();
        if(cmsChannel == null){
            throw new JeecgBootException("数据不存在");
        }
        return Result.ok(cmsChannelConvert.toVO(cmsChannel));
    }



}
